﻿using System;
using System.Windows.Forms;
using DesignerHelper.EventHandlers;
using DesignerHelper.Main;
using SpreadsheetGear;
using SpreadsheetGear.Commands;
using WorkbookDesigner.Main;
using SGForm = SpreadsheetGear.Windows.Forms;

namespace WorkbookDesigner.UI
{
    internal partial class DeleteCellsDialog : Form
    {
        private static DeleteCellsDialog _dcd = null;
        private IMainWindow Mw { get; set; }
        private SGForm.WorkbookView ActiveView { get; set; }
        private LockBookWorker LbwTrigger { get; set; }

        internal static void ShowStaticDialog(IMainWindow mw)
        {
            if (mw?.ActiveBookForm != null)
            {
                if (_dcd == null)
                {
                    _dcd = new DeleteCellsDialog(mw);
                    _dcd.Show(mw.GetForm());
                }
                else
                    _dcd.Activate();
            }
            else
                DesignerHelper.Help.MesBox.ErrorMessage("Could not open DeleteCellsDialog");
        }

        private DeleteCellsDialog()
        {
            InitializeComponent();
        }

        private DeleteCellsDialog(IMainWindow mw) : this()
        {
            Owner = mw.GetForm();
            Mw = mw;
        }

        private void AddEventHandler()
        {
            if (Mw.ActiveBookForm == null) return;
            Mw.FileOperator.CellBeginEditInternal += DisableDialog;
            Mw.FileOperator.CellCancelEditInternal += EnableDialog;
            Mw.FileOperator.CellEndEditInternal += EnableDialog;
            Mw.FileOperator.DisableUI += DisableDialog;
            Mw.FileOperator.EnableUI += EnableDialog;
        }
        private void RemoveEventHandler()
        {
            if (Mw.ActiveBookForm == null) return;
            Mw.FileOperator.CellBeginEditInternal -= DisableDialog;
            Mw.FileOperator.CellCancelEditInternal -= EnableDialog;
            Mw.FileOperator.CellEndEditInternal -= EnableDialog;
            Mw.FileOperator.DisableUI -= DisableDialog;
            Mw.FileOperator.EnableUI -= EnableDialog;
        }
        private void DisableDialog(object sender, EventArgs e)
        {
            if (e is SGForm.CellBeginEditEventArgs || e is NoFormLeftEventArgs)
                _disableDialog();
        }
        private void EnableDialog(object sender, EventArgs e)
        {
            if (e is SGForm.CellCancelEditEventArgs || e is SGForm.CellEndEditEventArgs || e is FormExistEventArgs)
                _enableDialog();
        }
        private void _enableDialog()
        {
            groupBox1.Enabled = true;
            buttonOK.Enabled = true;
            buttonDelete.Enabled = true;
        }
        private void _disableDialog()
        {
            groupBox1.Enabled = false;
            buttonOK.Enabled = false;
            buttonDelete.Enabled = false;
        }

        //工作表刷新
        //
        private bool RefreshSuccessed
        {
            get
            {
                RefreshWbv();
                return ActiveView != null;
            }
        }
        private void RefreshWbv()
        {
            ActiveView = Mw.ActiveView;
        }


        //窗口功能主体
        //
        //
        private DeleteShiftDirection SelectedDeleteOption
        {
            get
            {
                if (radioButtonShiftLeft.Checked || radioButtonEntCol.Checked)
                    return DeleteShiftDirection.Left;
                if (radioButtonShiftUp.Checked || radioButtonEntRow.Checked)
                    return DeleteShiftDirection.Up;
                return DeleteShiftDirection.Up;
            }
        }
        private IRange SelectedRange
        {
            get
            {
                if (radioButtonShiftLeft.Checked || radioButtonShiftUp.Checked)
                    return ActiveView.RangeSelection;
                if (radioButtonEntRow.Checked)
                    return ActiveView.RangeSelection.EntireRow;
                if (radioButtonEntCol.Checked)
                    return ActiveView.RangeSelection.EntireColumn;
                return ActiveView.RangeSelection;
            }
        }
        private void Init()
        {
            if (!RefreshSuccessed)
                return;
            LbwTrigger = LockBookWorker.GetWorker(ActiveView);
            LbwTrigger.DoWork(() =>
            {
                Command delete = new CommandRange.Delete(SelectedRange, SelectedDeleteOption);
                ActiveView.ActiveCommandManager.Execute(delete);
            });
        }

        //按钮及窗体事件
        //
        private void buttonOK_Click(object sender, EventArgs e)
        {
            Init();
            Close();
        }
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            Init();
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void DeleteCellsDialog_FormClosed(object sender, FormClosedEventArgs e)
        {
            _dcd = null;
            RemoveEventHandler();
        }
        private void DeleteCellsDialog_Load(object sender, EventArgs e)
        {
            radioButtonShiftUp.Checked = true;
            AddEventHandler();
        }


    }


}